package com.seleniti.enpareja.web.appbase.client.model;

import org.ioc.commons.utils.FormatterLogger;

import com.google.gwt.user.client.Window;
import com.seleniti.enpareja.controller.common.domain.UserAgent;

public class UserAgentResolver {

	private static final FormatterLogger logger = FormatterLogger.getLogger(UserAgentResolver.class);

	static {
		init();
	}

	private static boolean isFirefox;
	private static boolean isChrome;
	private static boolean isIE;
	private static boolean isOpera;
	private static boolean isSafari;
	private static boolean isAndroid;

	private static String version;
	private static UserAgent userAgent;

	private static void init() {
		String ua = Window.Navigator.getUserAgent().toLowerCase();

		logger.info("User agent: {0}", ua);

		boolean isWebKit = ua.indexOf("webkit") != -1;
		isOpera = (ua.indexOf("opera") != -1);
		isIE = !isOpera && (ua.indexOf("msie") != -1);
		isChrome = !isIE && ua.indexOf("chrome") != -1;
		isSafari = !isChrome && ua.indexOf("safari") != -1;
		isFirefox = !isWebKit && ua.indexOf("firefox") != -1;
		isAndroid = ua.indexOf("android") != -1;

		if (isAndroid) {
			userAgent = isAndroidBrowserEmbeddedInAndroidApp() ? UserAgent.ANDROID_EMBEDDED_BROWSER : UserAgent.ANDROID_BROWSER;
		} else

		if (isIE) {
			int startIdx = ua.indexOf("msie ") + "msie ".length();
			int endIdx = ua.indexOf(';', startIdx);
			version = ua.substring(startIdx, endIdx);
			userAgent = UserAgent.IE_BROWSER;
		} else

		if (isFirefox) {
			int startIdx = ua.indexOf("firefox/") + "firefox/".length();
			int endIdx = ua.indexOf(' ', startIdx);
			endIdx = (endIdx >= 0) ? endIdx : ua.length();

			version = ua.substring(startIdx, endIdx);
			userAgent = UserAgent.FIREFOX_BROWSER;
		} else

		if (isChrome) {
			int startIdx = ua.indexOf("chrome/") + "chrome/".length();
			int endIdx = ua.indexOf(' ', startIdx);
			endIdx = (endIdx >= 0) ? endIdx : ua.length();

			version = ua.substring(startIdx, endIdx);
			userAgent = UserAgent.CHROME_BROWSER;
		} else

		if (isOpera || isSafari) {
			int startIdx = ua.indexOf("version/") + "version/".length();
			int endIdx = ua.indexOf(' ', startIdx);
			endIdx = (endIdx >= 0) ? endIdx : ua.length();

			version = ua.substring(startIdx, endIdx);
			userAgent = isOpera ? userAgent = UserAgent.OPERA_BROWSER : UserAgent.SAFARI_BROWSER;
		}

		if (userAgent == null) {
			userAgent = UserAgent.UNKNOW;
		}
	}

	public static boolean isFirefox() {
		return isFirefox;
	}

	public static boolean isChrome() {
		return isChrome;
	}

	public static boolean isIE() {
		return isIE;
	}

	public static boolean isOpera() {
		return isOpera;
	}

	public static boolean isSafari() {
		return isSafari;
	}

	public static String getVersion() {
		return version;
	}

	public static boolean isAndroid() {
		return isAndroid;
	}

	public static UserAgent getUserAgent() {
		return userAgent;
	}
	
	private static native boolean isAndroidBrowserEmbeddedInAndroidApp () /*-{
		return $wnd.Android || window.Android;
	}-*/;
}
